package com.bjpowernode.services;

import com.bjpowernode.beans.Activity;
import com.bjpowernode.beans.User;
import com.bjpowernode.dto.ResultPageDTO;
import com.bjpowernode.mapper.ActivityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.UUID;


@Service
public class ActivityServicesImp implements ActivityServices {
    @Autowired
    ActivityMapper activityMapper;

    @Override
    public ArrayList<Activity> getAll(ResultPageDTO resultPageDTO) {

        //获取当前页
        int currentPage = resultPageDTO.getCurrentPage();
        //获取每页显示行数
        int rowsPerPage = resultPageDTO.getRowsPerPage();
        //计算起始页，提供SQL的limit使用
        int i = (currentPage - 1) * rowsPerPage;
        resultPageDTO.setCurrentPage(i);


        //查询总行数
        int count = activityMapper.getCount(resultPageDTO);
        //设置总行数
        resultPageDTO.setTotalRows(count);
        //假定总行数除每页显示行数没有余数
        resultPageDTO.setTotalPages(count / rowsPerPage);
        //如果有余数
        if (count % rowsPerPage != 0) {
            //设置总页数
            resultPageDTO.setTotalPages(count / rowsPerPage + 1);
        }

        return activityMapper.getAll(resultPageDTO);
    }

    @Override
    public Activity get(String id) {
        return activityMapper.get(id);
    }

    @Override
    public Activity getEditBefore(String id) {
        return activityMapper.getEditBefore(id);
    }

    @Override
    public boolean add(Activity activity) {
        String replace = UUID.randomUUID().toString().replace("-", "");
        activity.setId(replace);
        activity.setCreateTime(LocalDateTime.now().format( DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return activityMapper.add(activity) > 0 ? true : false;
    }

    @Override
    public boolean del(String[] ids) {
        return activityMapper.del(ids) > 0 ? true : false;
    }



    @Override
    public boolean edit(Activity activity) {
        activity.setEditTime(LocalDateTime.now().format( DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));


        return activityMapper.edit(activity) > 0 ? true : false;
    }
}
